package com.foreveross.ccjd.chart;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;

@SuppressLint("DrawAllocation")
public class ChartView extends View {
	private static final int HW = 130;
	private List<Float> mHList;
	private int mSpitHeight;
	private int mSpitWidth;
	
	int mW;
	int mH;
	float mYLable;
	float mLYline;
	private static final int TEXTSIZE = 40;
	public ChartView(Context context,int w,int h) {
		super(context);
		mSpitHeight = 60;
		mSpitWidth = 60;
		mW = w;
		mH = h;
		LayoutParams params = new LayoutParams(HW*(w-1)+mSpitWidth*2,HW*(h+1)+mSpitHeight);
		setLayoutParams(params);
		setBackgroundColor(Color.parseColor("#f5896d"));
		init();
	}


	private void init() {
		mHList = new ArrayList<Float>();
		for (int i = 0; i <=mH; i++) {
			float y = HW*i+mSpitHeight;
			mHList.add(y);
		}
		mLYline = HW*mH+mSpitHeight;
		mYLable = mLYline+HW*0.5f;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setAlpha(50);
		for (int i = 0; i < mHList.size(); i++) {
			float y = mHList.get(i);
			canvas.drawLine(mSpitWidth, y, HW*(mW-1)+mSpitWidth, y, paint);
		}
		Paint textPaint = new Paint();
		textPaint.setTextSize(TEXTSIZE);
		textPaint.setColor(Color.WHITE);
		for(int i=0;i<mW;i++)
		{
			canvas.drawLine(mSpitWidth+i*HW,mSpitHeight,i*HW+mSpitWidth,mLYline, paint);
			String text = ""+i;
			if(i<10)
			{
				text = "0"+i;
			}
			text =text+":00";
			int tw = text.length()*TEXTSIZE;
			int w = tw/4;
			canvas.drawText(text, i*HW-w+mSpitWidth, mYLable, textPaint);
		}
	}

}
